package com.example.admin.yuejian21.activity;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.fragment.JlbFragment;
import com.example.admin.yuejian21.fragment.PeixunFragment;
import com.example.admin.yuejian21.fragment.StFragment;
import com.example.admin.yuejian21.fragment.WoFragment;
import com.example.admin.yuejian21.fragment.ActivityFragment;
import com.example.admin.yuejian21.fragment.YjFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    private LocationManager locationManager;
    private LinearLayout mTabpeixun;
    private LinearLayout mTabjlb;
    private LinearLayout mTabyj;
    private LinearLayout mTabst;
    private LinearLayout mTabwo;


    private ImageButton mImgpeixun;
    private ImageButton mImgjlb;
    private ImageButton mImgyj;
    private ImageButton mImgst;
    private ImageButton mImgwo;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;
    private Fragment mTab04;
    private Fragment mTab05;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        // this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        LinearLayoutManager LL = new LinearLayoutManager(this);

        initView();//初始化
        initEvent();//初始化事件
        setSelect(2);//
    }

    private void initEvent()
    {
        mTabpeixun.setOnClickListener(this);
        mTabjlb.setOnClickListener(this);
        mTabyj.setOnClickListener(this);
        mTabst.setOnClickListener(this);
        mTabwo.setOnClickListener(this);
    }

    private void initView()
    {
        mTabpeixun = (LinearLayout) findViewById(R.id.id_tab_peixun);
        mTabjlb = (LinearLayout) findViewById(R.id.id_tab_jlb);
        mTabyj = (LinearLayout) findViewById(R.id.id_tab_yj);
        mTabst = (LinearLayout) findViewById(R.id.id_tab_st);
        mTabwo = (LinearLayout) findViewById(R.id.id_tab_wo);

        mImgpeixun = (ImageButton) findViewById(R.id.id_tab_peixun_img);
        mImgjlb = (ImageButton) findViewById(R.id.id_tab_jlb_img);
        mImgyj = (ImageButton) findViewById(R.id.id_tab_yj_img);
        mImgst = (ImageButton) findViewById(R.id.id_tab_st_img);
        mImgwo = (ImageButton) findViewById(R.id.id_tab_wo_img);
    }

    private void setSelect(int i)
    {

        
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new PeixunFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mImgpeixun.setImageResource(R.drawable.ic_peixun);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new JlbFragment()
                    ;transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                mImgjlb.setImageResource(R.drawable.ic_julebu);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new YjFragment();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mImgyj.setImageResource(R.drawable.ic_play);
                break;
            case 3:
                if (mTab04 == null)
                {
                    mTab04 = new StFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else
                {
                    transaction.show(mTab04);
                }
                mImgst.setImageResource(R.drawable.ic_shaitu);
                break;
            case 4:
                if (mTab05 == null)
                {
                    mTab05 = new WoFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else
                {
                    transaction.show(mTab05);
                }
                mImgwo.setImageResource(R.drawable.ic_wo);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }
        if (mTab04 != null)
        {
            transaction.hide(mTab04);
        }
        if (mTab05 != null)
        {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_peixun:
                setSelect(0);
                break;
            case R.id.id_tab_jlb:
                setSelect(1);
                break;
            case R.id.id_tab_yj:
                setSelect(2);
                break;
            case R.id.id_tab_st:
                setSelect(3);
                break;
            case R.id.id_tab_wo:
                setSelect(4);
                break;

            default:
                break;
        }
    }


    private void resetImgs()
    {
        mImgpeixun.setImageResource(R.drawable.ic_peixun1);
        mImgjlb.setImageResource(R.drawable.ic_julebu1);
        mImgyj.setImageResource(R.drawable.ic_play1);
        mImgst.setImageResource(R.drawable.ic_shaitu1);
        mImgwo.setImageResource(R.drawable.ic_wo1);
    }


}
